﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace balace_project_new
{
    class Data
    {
#region   struct data
        public struct revData
        {
            public int revDataID; 
            public string BalanceIP;
            public string BalanceID;
            public int Sequence;
            public float Weight;
            public float WeightErr;
            public float Err;
            public string Quality;      // "Ok" / "Lỗi"
            public TimeSpan Time;
            // public string Note;
        }

        public struct BalanceProperties   // Balance Properties
        {
            public string BalanceIP;
            public string BalanceID;            
            public string DrugID;
            public string DrugName;
            public string DrugManufacturer;
            public string DrugCatergory;
            public string DrugPackage;
            public float  Weight;
            public float  Err;
            // public string note;
        }
#endregion struct data              
        public BalanceProperties[] arrBalanceProperties = new BalanceProperties[17];
        public revData[] arrRevData = new revData[17];        

        // Get set-up parameters of 17 balances
        // return value in
        public void Get_BalanceProperties()
        {             
            int index;             
            balance_schemaDataSetTableAdapters.BalancePropertiesTableAdapter adt = new balance_schemaDataSetTableAdapters.BalancePropertiesTableAdapter();
            balance_schemaDataSet.BalancePropertiesDataTable dtb = new balance_schemaDataSet.BalancePropertiesDataTable();
            adt.Fill(dtb);
            int n = dtb.Rows.Count;            
            for (int i=0;i<n;i++)
            {
                int.TryParse(dtb.Rows[i]["ID"].ToString(),out index);
                index--;
                arrBalanceProperties[index].BalanceIP = dtb.Rows[i]["BalanceIP"].ToString();
                arrBalanceProperties[index].BalanceID = dtb.Rows[i]["ID"].ToString();
                arrBalanceProperties[index].DrugID = dtb.Rows[i]["DrugID"].ToString();
                arrBalanceProperties[index].DrugManufacturer = dtb.Rows[i]["Manufacturer"].ToString();
                arrBalanceProperties[index].DrugCatergory = dtb.Rows[i]["Catergory"].ToString();
                arrBalanceProperties[index].DrugName = dtb.Rows[i]["Name"].ToString();
                arrBalanceProperties[index].DrugPackage = dtb.Rows[i]["Package"].ToString();
                arrBalanceProperties[index].Weight = float.Parse(dtb.Rows[i]["Weight"].ToString());
                arrBalanceProperties[index].Err = float.Parse(dtb.Rows[i]["Err"].ToString());
            }
        }                 
        
        // Init Struct recvData for the 1st use
        // return array struct_recvdata last receive data of each balance
        public void Init_StructData()
        {            
        balance_schemaDataSetTableAdapters.recvdataTableAdapter adt = new balance_schemaDataSetTableAdapters.recvdataTableAdapter();
        balance_schemaDataSet.recvdataDataTable dtb = new balance_schemaDataSet.recvdataDataTable();
        adt.Fill_Latest(dtb);
        int Row = dtb.Rows.Count;                                            
        for(int i=0;i<Row;i++)
        {
            int index;
            int.TryParse(dtb[i]["Cân"].ToString(), out index);
            arrRevData[index - 1].BalanceID = dtb[i]["Cân"].ToString();
            int.TryParse(dtb[i]["Lần Cân"].ToString(), out arrRevData[index - 1].Sequence);
            float.TryParse(dtb[i]["Khối Lượng (g)"].ToString(), out arrRevData[index - 1].Weight);
            float.TryParse(dtb[i]["Sai Số (g)"].ToString(), out arrRevData[index - 1].WeightErr);
            float.TryParse(dtb[i]["Sai Số (%)"].ToString(), out arrRevData[index - 1].Err);
            arrRevData[index - 1].Quality = dtb[i]["Chất Lượng"].ToString();
            TimeSpan.TryParse(dtb[i]["Thời Gian"].ToString(), out arrRevData[index - 1].Time);
        }
        }
    }
}

